import { ref, onMounted, onBeforeUnmount } from 'vue'

export const useCountDown = (initialCount, callback) => {
  const count = ref(initialCount)
  const isOver = ref(false)
  let timerId = null

  onMounted(() => {
    timerId = setInterval(() => {
      count.value--

      if (count.value === 0) {
        clearInterval(timerId)
        isOver.value = true
        callback?.()
      }
    }, 1000)
  })

  onBeforeUnmount(() => {
    clearInterval(timerId)
  })

  return { count, isOver }
}
